//
//  QYXTabViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXTabViewController.h"

@interface QYXTabViewController ()
@property (nonatomic, strong) UIButton *tempButton;

@property (nonatomic, strong) UIView *indiocateView;
@end

@implementation QYXTabViewController

- (instancetype)initWithArray:(NSArray *)titleArray
{
    self = [super init];
    if (self) {
        
        self.tabBar.hidden = YES;
        //记录原tabBar的frame:
        CGRect oldFrame = self.tabBar.frame;
        //创建自己的view
        self.backView = [[UIView alloc] initWithFrame:oldFrame];
        self.backView.backgroundColor = [UIColor whiteColor];
        //添加在view上
        [self.view addSubview:_backView];
        
        //根据数组实际情况创建tabBar内容
        CGFloat width = [UIScreen mainScreen].bounds.size.width / titleArray.count;
        //循环创建按钮:
        for (NSInteger i = 0; i < titleArray.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor whiteColor];
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            button.frame = CGRectMake(width * i, 0, width - 2, oldFrame.size.height);
            button.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
            //添加tag值:
            button.tag = 1000 + i;
            //根据数组元素为按钮赋值:
            [button setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
            [self.backView addSubview:button];
            [button addTarget:self action:@selector(didClickedButton:) forControlEvents:UIControlEventTouchUpInside];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(button.frame.size.width + button.frame.origin.x + 1, 15, 1, oldFrame.size.height - 30)];
            label.backgroundColor = [UIColor lightGrayColor];
            [self.backView addSubview:label];
            
            if (i == 0) {
           
             [self didClickedButton:button];
      }
        }
        
        self.indiocateView = [[UIView alloc] initWithFrame:CGRectMake(0, _tempButton.frame.size.height + _tempButton.frame.origin.y - 4, 60, 2)];
        self.indiocateView.center = CGPointMake(_tempButton.center.x, _tempButton.center.y + 20);
        _indiocateView.backgroundColor = [UIColor lightGrayColor];
        [self.backView addSubview:_indiocateView];
      

    }
    return self;
}

#pragma mark --
- (void)didClickedButton:(UIButton *)sender
{
    NSLog(@"%ld", sender.tag);
    //根据点击按钮大小,切换视图控制器:
    self.selectedIndex = sender.tag - 1000;
    [self didChangeColorByButton:sender];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        self.indiocateView.frame = CGRectMake(0, sender.frame.size.height + sender.frame.origin.y - 4, 60, 2);
        self.indiocateView.center = CGPointMake(sender.center.x, sender.center.y + 20);

    }];
    
}
#pragma mark -- 改变button颜色
- (void)didChangeColorByButton:(UIButton *)button
{
    [self.tempButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.tempButton.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:15];
    self.tempButton = button;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
